package core.proto;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.LinkedList;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class TimeLine extends JPanel {

	private static final long serialVersionUID = -6382976816488406644L;
	private Collection<Rectangle> ticks;
	private Rectangle timeline;

	final int MARGIN = 20;
	final int THICKNESS = 10;
	final int LABELWIDTH = 100;
	final int LABELHEIGHT = 50;
	final int TICKHEIGHT = 50;
	final Color TIMELINE_COLOR = Color.black;
	final Color BG_COLOR = Color.black;
	private JLabel startLabel;
	private JLabel endLabel;
	private Collection<Point> marks; 

	public TimeLine(int startYear, int numYears){

		marks = new LinkedList<Point>();
		this.setSize(Util.getFavoritesWidth(), Util.getFavoritesHeight());
		this.setMinimumSize(new Dimension(Util.getFavoritesWidth(), Util.getFavoritesHeight()));

		this.setBackground(BG_COLOR);
		ticks = new LinkedList<Rectangle>();

		// Cache various parameters. 
		int timelineLen = Util.getFavoritesWidth() - 2 * MARGIN;
		int tickInterval = timelineLen / numYears;

		// Main timeline. 
		timeline = new Rectangle(MARGIN, Util.getFavoritesHeight() / 2 - THICKNESS / 2, timelineLen, THICKNESS);

		// Ticks.
		int tickPos = MARGIN;
		for(int i=0; i<numYears+1; ++i){
			Rectangle tick = makeTick(tickPos);
			ticks.add(tick);
			tickPos = tickPos + tickInterval;
		}
		startLabel = makeLabel(startYear, MARGIN);
		endLabel = makeLabel(startYear + numYears, MARGIN + numYears * tickInterval);

		this.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent me){
				boolean valid = false;
				// Detect whether click falls inside any of the rectangles. 
				if (timeline.contains(me.getPoint())){
					valid = true;
				}else{
					for(Rectangle tick : ticks){
						if (tick.contains(me.getPoint())){
							valid = true;
						}
					}
				}
				if (valid){
					// TODO draw waypoint with annotation.
					marks.add(me.getPoint());
					repaint();
				}
			}		
		});
	}


	/**
	 * 
	 * @param startX X position of where this tick is supposed to start. 
	 * @return
	 */
	private Rectangle makeTick(int startX){
		Rectangle tick = new Rectangle(startX - THICKNESS/2, Util.getFavoritesHeight() / 2 - TICKHEIGHT / 2,THICKNESS,TICKHEIGHT); 
		return tick;
	}

	private JLabel makeLabel(int year, int x){
		JLabel label = new JLabel(new Integer(year).toString());		
		label.setPreferredSize(new Dimension(LABELWIDTH,LABELHEIGHT));
		label.setLocation(new Point(x, Util.getFavoritesHeight() / 2 + TICKHEIGHT / 2));
		return label;
	}

	@Override public void paintComponent(Graphics g) {
		super.paintComponent(g);    // paints background
		for(Rectangle r : ticks){
			g.fillRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight());
		}
		g.fillRect((int) timeline.getX(), (int) timeline.getY(), (int) timeline.getWidth(), (int) timeline.getHeight());
		g.drawString(startLabel.getText(), MARGIN - THICKNESS, Util.getFavoritesHeight() / 2 - TICKHEIGHT);
		g.drawString(endLabel.getText(), endLabel.getX() - THICKNESS, Util.getFavoritesHeight() / 2 - TICKHEIGHT);
	}
}
